package com.shd.edanpei.ui.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseFragment;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.ui.fragment.ProductFragment;
import com.shd.edanpei.util.FragmentUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/3/29.
 * 我的产品
 */
public class MeProductsActivity extends BaseActivity {
//
//    @BindView(R.id.radio_group)
//    RadioGroup radioGroup;
//    @BindView(R.id.radio_button_left)
//    RadioButton rbtLeft;
//    @BindView(R.id.radio_button_right)
//    RadioButton rbtRight;

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_headline_right)
    ImageView ivRight;

    private FragmentUtil mFragmentUtil;
    private Dialog dialog;
    private Bundle bundle = new Bundle();

    @Override
    protected int setLayout() {
        return R.layout.activity_meproducts;
    }

    @Override
    protected BasePresenterImpl bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("银行入口");
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageDrawable(getResources().getDrawable(R.drawable.ic_product_add));
        mFragmentUtil = new FragmentUtil(getSupportFragmentManager(), R.id.frame_layout);
        changeTab(1);
    }


    @Override
    protected void initListener() {
        ivRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    private void changeTab(int id) {
        if (!mFragmentUtil.hasFragment(id)) {
            mFragmentUtil.add2Map(newFragment(id), id);
        }
        mFragmentUtil.changeTab(id);
    }

    /**
     * 根据id，创建对应的fragment
     */
    private BaseFragment newFragment(int id) {
        switch (id) {
            case 1:
                return ProductFragment.newInstance(1);
            case R.id.radio_button_right:
                return ProductFragment.newInstance(0);
            default:
                return null;

        }
    }


    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }


    /**
     * 新增产品
     */
    private void showDialog() {
        if (dialog == null) {
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_products, null);
            final RadioGroup radioGroup = view.findViewById(R.id.radio_group);
            view.findViewById(R.id.tv_sure)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            bundle.putInt(Constant.TYPE, radioGroup.getCheckedRadioButtonId() == R.id.radio_button1 ? Constant.CAR : Constant.HOUSE);
                            startActivity(AddProductActivity.class, bundle);
                            dialog.dismiss();
                        }
                    });
            dialog = new Dialog(this, R.style.dialog);
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(true);
        }
        dialog.show();
    }

}
